package jiemian1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Register {
    private JTextField idtxt;
    private JPasswordField passwordtxt;
    private JPasswordField passwordsure;
    private JButton registerbtn;
    private JLabel id;
    private JLabel password;
    private JLabel surepass;
    public JPanel registerpane;
    private JButton deletebtn;
    public static JFrame frame;
    private static String userId;
    private static String passwd;
    private static String surepwd;
    public Register() {



        registerbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                JOptionPane.showMessageDialog(null,"3534354fdfdsf");
                boolean a=true;
                System.out.println(a);
                while(a) {
                    if (e.getSource() == registerbtn) {
                        userId = idtxt.getText();
                        passwd = passwordtxt.getPassword().toString();

                        surepwd = passwordsure.getPassword().toString();

                        if (!passwd.equals(surepwd)) {
                            JOptionPane.showMessageDialog(null, "您的两次密码不一致，请重新输入");
                            break;
                        }
                        a=false;
                        //之后进入密保页面，并关闭注册页面.
                        JFrame frame1 = new JFrame("设置密保问题");
                        frame1.setContentPane(new Set(frame1,userId,passwd).set);
                        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame1.setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width) / 2) - 300,
                                ((Toolkit.getDefaultToolkit().getScreenSize().height) / 2) - 300, 600, 600);
                        frame1.pack();
                        frame1.setVisible(true);
                        frame1.setSize(500, 300);
                        frame.dispose();
                    }
                }

            }
        });
        deletebtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
    }

    public Register(JFrame x){
        super();
        frame=x;

        registerbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean a=true;
                while(a) {
                    if (e.getSource() == registerbtn) {
                        userId = idtxt.getText();
                        passwd = new String(passwordtxt.getPassword());
                        surepwd = new String(passwordsure.getPassword());
                        if (!passwd.equals(surepwd)) {
                            JOptionPane.showMessageDialog(null, "您的两次密码不一致，请重新输入");
                            break;
                        }
                        a=false;
                        //之后进入密保页面，并关闭注册页面.
                        JFrame frame1 = new JFrame("设置密保问题");
                        frame1.setContentPane(new Set(frame1,userId,passwd).set);
                        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame1.setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width) / 2) - 300,
                                ((Toolkit.getDefaultToolkit().getScreenSize().height) / 2) - 300, 600, 600);
                        frame1.pack();
                        frame1.setVisible(true);
                        frame1.setSize(500, 300);
                        frame.dispose();
                    }
                }
//                if (e.getSource() == registerbtn) {
////                    //点击注册按钮，将信息插入至数据库，
////                    //之后进入密保页面，并关闭注册页面.
////                    JFrame frame1 = new JFrame("设置密保问题");
////                    frame1.setContentPane(new Set(frame1).set);
////                    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
////                    frame1.setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width)/2)-300,
////                            ((Toolkit.getDefaultToolkit().getScreenSize().height)/2)-300,600,600);
////                    frame1.pack();
////                    frame1.setVisible(true);
////                    frame1.setSize(500,300);
////                    frame.dispose();
////                }


            }
        });
        deletebtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
    }

    public static void main(String[] args) {



        //JFrame frame = new JFrame("Register");
        frame.setTitle("注册");

        frame.setContentPane(new Register().registerpane);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        frame.setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width) / 2) - 300,
                ((Toolkit.getDefaultToolkit().getScreenSize().height) / 2) - 300, 600, 600);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(400,300);
    }


}
